<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { LineModel } from '#/api/master/models';
import type { OperationModel } from '#/api/process/models';

import { onMounted, ref } from 'vue';

import { Page, useVbenModal } from '@vben/common-ui';

import { Button, List, ListItem, message, Tag } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getLineListApi } from '#/api/master/line';
import {
  deleteOperationApi,
  getOperationPagedListApi,
} from '#/api/process/operation';
import { $t } from '#/locales';

import { useColumns } from './data';
import Form from './modules/form.vue';

const lines = ref<LineModel[]>([]);
const lineIdSelected = ref<number>(0);

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

/**
 * 创建工序
 */
function onCreate() {
  formModalApi.setData({ lineId: lineIdSelected.value }).open();
}

/**
 * 编辑工序
 * @param row
 */
function onEdit(row: OperationModel) {
  formModalApi.setData(row).open();
}

/**
 * 删除工序
 * @param row
 */
function onDelete(row: OperationModel) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.opName]),
    duration: 0,
    key: 'action_process_msg',
  });
  deleteOperationApi(row.id)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.opName]),
        key: 'action_process_msg',
      });
      refreshGrid();
    })
    .catch(() => {
      hideLoading();
    });
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({ code, row }: OnActionClickParams<OperationModel>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useColumns(onActionClick),
    border: true,
    stripe: true,
    pagerConfig: {},
    proxyConfig: {
      autoLoad: false,
      ajax: {
        query: async ({ page }) => {
          return await getOperationPagedListApi({
            pageNumber: page.currentPage,
            pageSize: page.pageSize,
            lineId: lineIdSelected.value,
          });
        },
      },
    },
  } as VxeTableGridOptions,
});

async function lineItemClick(item: LineModel) {
  lineIdSelected.value = item.id;
  gridApi.setLoading(true);
  refreshGrid();

  setTimeout(() => {
    gridApi.setLoading(false);
  }, 300);
}

/**
 * 刷新表格
 */
function refreshGrid() {
  gridApi.query();
}

onMounted(async () => {
  lines.value = await getLineListApi();
});
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="refreshGrid()" />
    <div class="flex h-full w-full">
      <List
        :data-source="lines"
        :split="false"
        bordered
        class="w-2/12 overflow-auto"
        size="small"
      >
        <template #header>
          <div>生产线</div>
        </template>
        <template #renderItem="{ item }">
          <ListItem>
            <div
              :class="[{ isActive: item.id === lineIdSelected }]"
              class="w-full cursor-pointer rounded-sm px-1"
              @click="lineItemClick(item)"
            >
              {{ item.lineName }}
            </div>
          </ListItem>
        </template>
      </List>
      <Grid class="ml-2 w-10/12">
        <template #toolbar-actions>
          <Button
            :disabled="!lineIdSelected"
            type="primary"
            @click="onCreate()"
          >
            新增工序
          </Button>
        </template>
        <template #workstations="{ row }">
          <Tag v-for="item in row.workstations" :key="item.id">
            {{ item.wsCode }}
          </Tag>
        </template>
      </Grid>
    </div>
  </Page>
</template>

<style scoped>
.isActive {
  background-color: #1890ff;
}
</style>
